package com.hengxinguotong.zhihuichengjian.ui;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.DeviceListPagerAdapter;
import com.hengxinguotong.zhihuichengjian.ui.app.BaseFragment;
import com.hengxinguotong.zhihuichengjian.fragment.EnergyFragment;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.view.NoScrollViewPager;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/9/12.
 */

public class EnergyActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.tv_back)
    HXTextView tvBack;
    @BindView(R.id.water_elec_top)
    RadioGroup water_elec_top;
    @BindView(R.id.elec_rb)
    RadioButton elecRb;
    @BindView(R.id.water_rb)
    RadioButton waterRb;
    @BindView(R.id.tv_detail)
    HXTextView tvDetail;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.view_pager)
    NoScrollViewPager viewPager;

    private EnergyActivity mActivity;
    private List<String> mTitleDataList;
    private ArrayList<BaseFragment> fragments;
    private EnergyFragment dayFragment, weekFragment, monthFragment;
    private int currentPage = 0;
    public static final String DAY = "1";
    public static final String WEEK = "2";
    public static final String MONTH = "3";
    public static final String ELEC = "1";
    public static final String WATER = "2";
    private DeviceListPagerAdapter deviceListPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_energy);
        ButterKnife.bind(this);
        mActivity = this;
        initView();
    }

    private void initView() {
        initMeasure();

        viewPager.setOffscreenPageLimit(3);
        fragments = new ArrayList<BaseFragment>();
        dayFragment = EnergyFragment.newInstance(DAY);
        dayFragment.setEquipmentType(ELEC);
        fragments.add(dayFragment);
        weekFragment = EnergyFragment.newInstance(WEEK);
        weekFragment.setEquipmentType(ELEC);
        fragments.add(weekFragment);
        monthFragment = EnergyFragment.newInstance(MONTH);
        monthFragment.setEquipmentType(ELEC);
        fragments.add(monthFragment);
        deviceListPagerAdapter = new DeviceListPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(deviceListPagerAdapter);

        mTitleDataList = new ArrayList<String>();
        mTitleDataList.add("日");
        mTitleDataList.add("7天");
        mTitleDataList.add("月");

        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mTitleDataList == null ? 0 : mTitleDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mTitleDataList.get(index));
                simplePagerTitleView.setTextSize(18);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.color_adadad));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.color_ffc065));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        currentPage = index;
                        viewPager.setCurrentItem(currentPage);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.color_ffc065));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerDrawable(new ColorDrawable() {
            @Override
            public int getIntrinsicWidth() {
                return Utils.dip2px(mActivity, 40);
            }
        });

        final FragmentContainerHelper fragmentContainerHelper = new FragmentContainerHelper(magicIndicator);
        fragmentContainerHelper.setInterpolator(new OvershootInterpolator(2.0f));
        fragmentContainerHelper.setDuration(300);
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                fragmentContainerHelper.handlePageSelected(position);
                currentPage = position;

            }
        });
        currentPage = 0;
        viewPager.setCurrentItem(currentPage);
    }

    @OnClick({R.id.tv_back, R.id.tv_detail, R.id.elec_rb, R.id.water_rb})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_detail:
                showPopupWindow(v);
                break;
            case R.id.elec_rb:
                for (BaseFragment baseFragment : fragments) {
                    EnergyFragment energyFragment = (EnergyFragment) baseFragment;
                    energyFragment.setEquipmentType(ELEC);
                    energyFragment.reLoad();
                }
                tvDetail.setText("分表");
                break;
            case R.id.water_rb:
                for (BaseFragment baseFragment : fragments) {
                    EnergyFragment energyFragment = (EnergyFragment) baseFragment;
                    energyFragment.setEquipmentType(WATER);
                    energyFragment.reLoad();
                }
                tvDetail.setText("分表");
                break;
            case R.id.tv_high:
                for (BaseFragment baseFragment : fragments) {
                    EnergyFragment energyFragment = (EnergyFragment) baseFragment;
                    energyFragment.setSeperate();
                }
                pw.dismiss();
                tvDetail.setText("分表");
                break;
            case R.id.tv_sub:
                for (BaseFragment baseFragment : fragments) {
                    EnergyFragment energyFragment = (EnergyFragment) baseFragment;
                    energyFragment.setTotal();
                }
                pw.dismiss();
                tvDetail.setText("汇总");
                break;
        }
    }

    private PopupWindow pw;
    private int width;// 屏幕的宽度
    private int height;// 屏幕的高度
    private void initMeasure() {
        width = getResources().getDisplayMetrics().widthPixels;
        height = getResources().getDisplayMetrics().heightPixels;
    }
    //弹出一个PopupWindow的窗体
    private void showPopupWindow(View view) {
        float w = (float) (width / 6.3);
        View v = LayoutInflater.from(this).inflate(R.layout.video_popup_window, null);
        TextView tv_high = (TextView) v.findViewById(R.id.tv_high);
        tv_high.setText("分表");
        tv_high.setOnClickListener(this);
        TextView tv_sub = (TextView) v.findViewById(R.id.tv_sub);
        tv_sub.setText("汇总");
        tv_sub.setOnClickListener(this);
        TextView tv_standard = (TextView) v.findViewById(R.id.tv_standard);
        tv_standard.setVisibility(View.GONE);
        pw = new PopupWindow(v, view.getWidth() + 30, (int) w);
        pw.setFocusable(true);
        pw.setBackgroundDrawable(new BitmapDrawable());
        pw.showAsDropDown(view, 5, 10);

    }

    public void setState(String state){
        tvDetail.setText(state);
    }

}

